//
//  AddressViewController.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/8.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

class AddressViewController: MeBaseViewController,UIPickerViewDelegate,UIPickerViewDataSource {
    
    
    /**
     用户动态标识ID
     add:添加；upd:更新；del:删除；def:默认；list:列表
     0:实体地址；1：虚拟地址   （ac为list时不需要）
     省份
     城市
     区、县
     详细地址
     收货人姓名
     收货人手机
     收货人QQ
     默认地址（1：是；0：否）

     */
    
    //用户名
    @IBOutlet weak var tfName: UITextField!
    
    //联系方式
    @IBOutlet weak var tfNum: UITextField!
    
    //省份
    @IBOutlet weak var province: UITextField!
    
    //城市
    @IBOutlet weak var city: UITextField!
    
    //地区
    @IBOutlet weak var cityLocal: UITextField!
    
    //详细地址
    @IBOutlet weak var detailAddress: UITextField!
    
    //默认地址
    @IBOutlet weak var defaultAddress: UITextField!

    //开关
    @IBOutlet weak var swi: UISwitch!
    

    let addressArray = ["交易明细","中奖纪录","收获地址","分享有礼"]
    let cityArray = ["收获地址","分享有礼"]
    var pickerView = UIPickerView()


    override func viewDidLoad() {
        super.viewDidLoad()

            // Do any additional setup after loading the view.
        self.title = "我的收货地址"
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

// MARK:- 设置按钮事件
extension AddressViewController {
    
    //选择城市
    @IBAction func selectCity(_ sender: UIButton) {
        setUpData()
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("选择城市")))")
    }
    
    //选择省份
    @IBAction func selectProvince(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("选择省份")))")
    }
    
    //选择地区
    @IBAction func selectArea(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("选择地区")))")
    }
    
    //保存信息
    @IBAction func saveInfo(_ sender: UIButton) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("保存信息")))")
        infoUpdate()

    }
}

// MARK:- datePickerView
extension AddressViewController {
    func setUpData() -> Void {
        pickerView = UIPickerView(frame: CGRect(x: 0, y: UIScreen.main.bounds.height - 130, width: UIScreen.main.bounds.width, height: 130))
        pickerView.backgroundColor = UIColor.blue
        view.addSubview(pickerView)
        
        self.pickerView.delegate = self
        self.pickerView.dataSource = self

    }
}

// MARK:- 信息提交
extension AddressViewController {
    func infoUpdate() -> Void {
        
        if localSave.object(forKey: userToken) as? String != nil {
            let token = localSave.object(forKey: userToken) as! String
            print(token)
            
            //        let url = "\(addressUrl)\("?")\("token=")\(uToken)\("&")\("ac=")\("zdx")\("&")\("ty=")\("0")\("&")\("province=")\("福建省")\("&")\("area=")\("鼓楼区")\("&")\("addrs=")\("软件园")\("&")\("shrname=")\("啊不错的")\("&")\("shrphone=")\("18905036476")\("&")\("default=")\("1")\("&")\("qq=")\("946737816")"
            
            let url = "\(addressUrl)\("?")\("token=")\(token)\("&")\("ac=")\("zdx")\("&")\("ty=")\("0")"
            
            print(url)
                    NetTool.shareInstance.postWithPath(path: url, paras: nil, success: { (data) in
                        print(data)
                    }, failure: { (error) in
                        print(error.localizedDescription)
                    })

        } else {
            print("token不存在")
            return
        }
            

    }
}

extension AddressViewController {
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.addressArray.count
    }
    
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        //        print(cityDataSource.count)
        //        return cityDataSource.count
        return 1
    }
    
    //设置选择框各选项的内容，继承于UIPickerViewDelegate协议
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int,
                    forComponent component: Int) -> String? {
        
        
        if(component == 0)
        {
            return self.addressArray[row]
        }
        else
        {
            return self.cityArray[row]
        }
        
    }
    
    //监听选择的行列
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("选择的行")))")
        print(row)
        
        print("\((#file as NSString).lastPathComponent):(\(#line)):(\("选择的列")))")
        print(component)
    }
}
